package org.bluedream.core.module.sys.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.bluedream.core.module.sys.entity.Role;
import org.bluedream.core.module.sys.entity.User;

import java.util.List;

public interface UserDao extends BaseMapper<User> {

    @Select("select * from sys_user where status = '0'")
    List<User> selectAll();

    @Select("select a.id as id " +
            "from sys_organization a , sys_org_relation b , sys_role role, sys_role_relation RoleRelation , sys_user e " +
            "where  e.login_code = #{login_Code} and a.id = #{id}" +
            " and a.id = b.org_id " +
            " and b.type = 'role' and b.entity_id = role.id and role.id = RoleRelation.role_id and RoleRelation.type = 'user' " +
            " and RoleRelation.entity_id = e.id ")
    List<String> checkLoginOrg(@Param("login_Code")String loginCode , @Param("id")String orgCode);

    @Select("select a.role_name as roleName " +
            "             from sys_role a  , sys_role_relation b , sys_user c " +
            "             where a.status = '0' and b.status = '0' and c.status = '0' " +
            "             and a.id = b.role_id and b.entity_id = c.id and c.id = #{id} and b.type = 'user' " +
            "             order by a.role_sort")
    List<String> getRoleIds(User user);

    @Select("select a.permission as permission from sys_menu a , sys_role_relation b , sys_role_relation c , sys_user d " +
            "       where a.status = '0' and b.status = '0' and c.status = '0' and d.status = '0' " +
            "       and a.id = b.entity_id and b.role_id = c.role_id and c.entity_id = d.id " +
            "       and b.type = 'menu' and c.type = 'user' " +
            "       and a.PERMISSION is not null" +
            "       and d.id = #{id}" +
            " order by a.tree_sorts , a.tree_sort")
    List<String> getMenuPermissions2String(User user);

    @Select("select a.* from sys_user a , sys_role_relation b , sys_role c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            " and a.id = b.entity_id and b.type = 'user' " +
            " and b.role_id = c.id and c.id = #{roleId} " +
            "order by a.login_code")
    List<User> getUsersByRoleIdOfIsChecked(String roleId);

    @Select("select a.* from sys_user a , t_dep_user b , tDepItem c " +
            "where a.status = '0' and b.status = '0' and c.status = '0' " +
            " and a.id = b.loginCode " +
            " and b.depCode = c.id and c.id = #{depItemId} " +
            "order by a.login_code")
    List<User> getUsersByDepItemIdOfIsChecked(String depItemId);
/*
    @Select("select id , login_code , user_name from sys_user where login_code = 'sa'")
    User getSystem();
    */
    @Select("select distinct * from sys_user " +
            "${ew.customSqlSegment} ")
    IPage<User> selectPage(IPage<User> page, @Param("ew") Wrapper<User> queryWrapper);

    @Override
    @Select("select distinct * from sys_user " +
            "${ew.customSqlSegment} ")
    List<User> selectList(@Param("ew") Wrapper<User> queryWrapper);
}
